package com.vitria.m3oui.rest.app.mgr;

import java.io.FileInputStream;
import java.io.IOException;

import org.restlet.data.Form;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;

import com.qilin.hammer.util.BaseRestService;

public class ConcurrentKpiTest extends BaseRestService{
	public static String kpiUrlBase = URL_PREFIX + "/app/sla/user/vtbaadmin/kpi/SLA";
    public static String saveOp = "?op=save";
	public static String deployOp = "?op=deploy";
	public static String startOp = "?op=start&needDeploy=true";
	public static String stopOp = "?op=stop";
	public static String deleteOp = "?op=delete";
	public static String kpi1ContentFile = "D:/work/main/yoda/m3o/ui/devtests/junit/app.memory/resources/kpi1Content.txt";
	private Integer threshold = 0;
	
    public ConcurrentKpiTest(String name) {
        super(name);
    }
    
    public void testLoop() throws Exception {
    	final String document = readFromFile(kpi1ContentFile);
    	for(int i=0; i<100000; i++){
    		synchronized(threshold){
    			if(threshold == 0){
    				threshold = 5;
    				System.out.println("Loop:"+i);
    			} else {
    				Thread.sleep(1000);
    				i--;
    				continue;
    			}
    		}
    		for(int j=0; j<5; j++){
    			final int k = j;
    			new Thread(){
    				public void run(){
    					try {
							operationKPIModel(String.valueOf(k), document);
				    		synchronized(threshold){
				    			threshold--;
				    		}
						} catch (IOException e) {
							e.printStackTrace();
						}
						
    				}
    			}.start();
    		}
    	}
    }
    
    private void operationKPIModel(String index, String document) throws IOException{
 	 //KPI model test 
		String opString;
		String properties = "<properties><property name='tags' value=''/></properties>";
		
		Form form = new Form();
		form.add("_RESOURCE_Type", "kpi");
		form.add("document", document);
		form.add("properties", properties);
		form.add("space", "private");
		
		 // create KPI model SLA{$i}
		opString = kpiUrlBase + index + saveOp;
		ClientResource resource = createSecureClientResource(opString);
		Representation rep = resource.post(form.getWebRepresentation());
		String text = rep.getText();
		System.out.println("create KPI"+ index +" model:"+text);
		
		opString = kpiUrlBase + index  + startOp;
		resource = createSecureClientResource(opString);
		rep = resource.get();
		text = rep.getText();
		System.out.println("deploy and start KPI"+ index +" model:"+text);
		
		// stop KPI model
		opString = kpiUrlBase + index  + stopOp;
		resource = createSecureClientResource(opString);
		rep = resource.get();
		text = rep.getText();
		System.out.println("stop KPI"+ index +" model:"+text);
		
		// delete KPI model
		opString = kpiUrlBase + index  + deleteOp;
		resource = createSecureClientResource(opString);
		rep = resource.get();
		text = rep.getText();
		System.out.println("delete KPI"+ index +" model:"+text);
    }
    
    private static String readFromFile(String fileName) {
		try {
			StringBuilder sb = new StringBuilder();
			FileInputStream fstream = new FileInputStream(fileName);
			java.io.DataInputStream is = new java.io.DataInputStream(fstream);
			java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(is));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				sb.append(strLine);
			}
			is.close();
			return sb.toString();
		}
		catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}
		return null;
	}
}
